﻿// 功能：控件、窗口等UI元素的接口
// 描述：
// 作者：温伟鹏
// 日期：2010/5/24 22:30:03
using System;
using System.Drawing;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 控件、窗口的基类(抽象的UI元素)
    /// </summary>
    public abstract class UIElement : IUIElement
    {
        #region 事件
        /// <summary>
        /// 当控件大小被改变时触发此事件
        /// </summary>
        public event EventHandler SizeChanged;
        /// <summary>
        /// 当控件位置改变后触发此事件
        /// </summary>
        public event EventHandler LocationChanged;
        #endregion

        #region 私有变量
        /// <summary>
        /// UI元素是否已经被销毁
        /// </summary>
        private bool isDisposed;
        /// <summary>
        /// 控件集合
        /// </summary>
        private ControlCollection controls;
        /// <summary>
        /// UI元素实例指针
        /// </summary>
        private IntPtr pObj;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取当前UI元素是否已经被释放
        /// </summary>
        internal bool IsDisposed
        {
            get
            {
                return this.isDisposed;
            }
        }
        /// <summary>
        /// 获取UI元素的实例指针
        /// </summary>
        public IntPtr Handle
        {
            get { return pObj; }
            protected set
            {
                this.pObj = value;
            }
        }
        /// <summary>
        /// 获取一个值，该值指示此UI元素是否有与它关联的实例指针。
        /// </summary>
        public virtual bool IsHandleCreated
        {
            get
            {
                return (Handle != IntPtr.Zero);
            }
        }
        /// <summary>
        /// 获取控件集合
        /// </summary>
        public ControlCollection Controls
        {
            get
            {
                if (controls == null)
                {
                    controls = new ControlCollection(this);
                }
                return controls;
            }
        }
        /// <summary>
        /// 获取UI元素是否含有子元素
        /// </summary>
        public bool HasChildren
        {
            get
            {
                return Controls.Count > 0;
            }
        }
        /// <summary>
        /// 获取或设置UI元素的前景色（文本颜色）
        /// </summary>
        public virtual Color ForeColor { get; set; }
        /// <summary>
        /// 获取或设置UI元素的大小
        /// </summary>
        public virtual Size Size
        {
            get;
            set;
        }
        /// <summary>
        /// 获取或设置UI元素的位置
        /// </summary>
        public virtual Point Location
        {
            get;
            set;
        }
        /// <summary>
        /// 获取或设置UI元素的高度
        /// </summary>
        public virtual int Height { get; set; }
        /// <summary>
        /// 获取或设置UI元素的宽度
        /// </summary>
        public virtual int Width { get; set; }
        /// <summary>
        /// 获取UI元素客户区域所在矩形
        /// </summary>
        public abstract Rectangle ClientRectangle { get; }
        /// <summary>
        /// 获取UI元素的客户区域大小
        /// </summary>
        public virtual Size ClientSize
        {
            get
            {
                return Size;
            }
        }
        /// <summary>
        /// 获取或设置UI元素显示的文本
        /// </summary>
        public virtual string Text
        {
            get;
            set;
        }
        #endregion

        #region 函数
        /// <summary>
        /// 向UI元素中添加子控件（由ControlCollection调用）
        /// </summary>
        /// <param name="control"></param>
        internal abstract void AddChild(Control control);
        /// <summary>
        /// 从UI元素中移除指定的子控件（由ControlCollection调用）
        /// </summary>
        /// <param name="control"></param>
        internal abstract void RemoveChild(Control control);
        /// <summary>
        /// 清空此UI元素中的所有子控件
        /// </summary>
        internal virtual void RemoveAllChildren()
        {

        }
        /// <summary>
        /// 处理MzFc消息
        /// </summary>
        /// <param name="message">消息值</param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        protected virtual void WndProc(uint message, uint wParam, uint lParam)
        {
        }
        #endregion

        #region 事件函数
        /// <summary>
        /// 引发 SizeChanged 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnSizeChanged(EventArgs e)
        {
            if (this.SizeChanged != null)
                this.SizeChanged(this, e);
        }
        /// <summary>
        /// 引发 LocationChanged 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnLocationChanged(EventArgs e)
        {
            if (this.LocationChanged != null)
                this.LocationChanged(this, e);
        }
        #endregion

        #region IUIElement 成员
        /// <summary>
        /// 更新UI元素
        /// </summary>
        public abstract void Update();
        /// <summary>
        /// 使UI元素无效
        /// </summary>
        public abstract void Invalidate();
        /// <summary>
        /// 使UI元素的指定区域无效
        /// </summary>
        /// <param name="rectUpdate">要在下次重绘时进行重绘的区域</param>
        public abstract void Invalidate(Rectangle rectUpdate);

        #endregion

        #region IDisposable 成员
        /// <summary>
        /// 销毁资源
        /// </summary>
        public void Dispose()
        {
            if (isDisposed)
                return;

            Dispose(true);

            isDisposed = true;
        }
        /// <summary>
        /// 销毁资源
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (Controls.Count > 0)
            {
                Controls.Clear();
            }
        }

        #endregion
    }
}
